<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_user.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_user.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_security.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_sql_convert.php';


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_users {
    
    public static function save_user($id, $first_name, 
                $last_name, $phone_number, $email, $password,
                $user_cinema_id) {
        $dao_user = new dao_user();
        if (empty($user_cinema_id)) {
            $user_cinema_id = 0;
        }
        
        $inserted_id = $dao_user->save($id, $first_name, 
                $last_name, $phone_number, $email, $password, 2, $user_cinema_id, "Admin Created");
        
        return $dao_user->active($inserted_id);
    }
    
    public static function delete_user($user_id) {
        $dao_user = new dao_user();
        return $dao_user->delete($user_id);
    }
    
    public static function get_user_by_id($user_id) {
        $dao_user = new dao_user();
        return $dao_user->get_by_id($user_id);
    }
    
    public static function get_all_users() {
        $dao_user = new dao_user();
        return $dao_user->get_all();
    }
    
    public static function get_all_users_with_now_showing() {
        $dao_user = new dao_user();
        $status_id = 2; //for now showing
        return $dao_user->get_all_by_status_id($status_id);
    }
    
    public static function get_all_users_with_comming_soon() {
        $dao_user = new dao_user();
        $status_id = 1; //for comming soon
        return $dao_user->get_all_by_status_id($status_id);
    }
    
    public static function get_all_user_statuses() {
        $dao_user_status = new dao_user_status();
        return $dao_user_status->get_all();
    }
    
    public static function get_default_user_statuses() {
        $dao_user_status = new dao_user_status();
        $status = $dao_user_status->get_first();
        if (!empty($status['name'])) {
            return $status['name'];
        }
        return "";
    }
    
    public static function get_status_id_from_name($name) {
        $dao_user_status = new dao_user_status();
        $status = $dao_user_status->get_id_by_name($name);
        return $status['id'];
    }
    
    public static function validate_user_fields($user_id, $first_name, 
                $last_name, $phone_number, $email, $password,
                $password_confirm) {
        $error_show = "";
        if (empty($first_name)) {
            $error_show .= "<li>Please fill First Name.</li>";
        }
        if (empty($last_name)) {
            $error_show .= "<li>Please fill Last Name.</li>";
        }
        if (empty($phone_number)) {
            $error_show .= "<li>Please fill Phone Number.</li>";
        }
        if (empty($email)) {
            $error_show .= "<li>Please fill Email.</li>";
        }
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error_show .= "<li>Email invalid.</li>";
        }
        if (api_security::email_exists($email) && $user_id == 0) {
            $error_show .= "<li>Email already exists.</li>";
        }
        if (empty($password)) {
            $error_show .= "<li>Please fill Password.</li>";
        }
        if (empty($password_confirm) || $password_confirm !== $password) { //match password
            $error_show .= "<li>You have to confirm password exactly!</li>";
        }
        return $error_show;
    }
    
}
